package com.xywy.tangandroid.activity;

import java.util.HashMap;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.xywy.tangandroid.R;
import com.xywy.tangandroid.base.BaseActivity;
import com.xywy.tangandroid.bean.User;
import com.xywy.tangandroid.util.MD5;
import com.xywy.tangandroid.util.UIUtil;
import com.xywy.tangandroid.vollery.OnVolleyResponseListener;
import com.xywy.tangandroid.vollery.PostRequest;

public class VerifyOriginPasswordAct extends BaseActivity {

	private RelativeLayout back;
	private TextView middle_title;
	private EditText et_origin_psw;
	private RelativeLayout validation_psw_button;
	private String psw;
	private String phonenum;

	@Override
	protected void initView(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		setContentView(R.layout.act_verify_origin_password);

		back = (RelativeLayout) findViewById(R.id.left_img);
		middle_title = (TextView) findViewById(R.id.middle_title);
		middle_title.setText("验证原密码");
		et_origin_psw = (EditText) findViewById(R.id.et_origin_psw);
		validation_psw_button = (RelativeLayout) findViewById(R.id.validation_psw_button);
	}

	@Override
	protected void initListener() {
		// TODO Auto-generated method stub
		back.setOnClickListener(this);
		validation_psw_button.setOnClickListener(this);

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		super.onClick(v);
		switch (v.getId()) {
		case R.id.left_img:
			finish();
			break;
		case R.id.validation_psw_button:
			psw = et_origin_psw.getText().toString();
			if (psw.length() < 6) {
				Toast.makeText(context, "请输入密码", Toast.LENGTH_SHORT).show();
				return;
			} else {
				verifyOriginPsw();
			}
			break;

		default:
			break;
		}
	}

	private void verifyOriginPsw() {
		// TODO Auto-generated method stub

		String url = "http://api.wws.xywy.com/index.php?" + "&tag=kbb&sign="
				+ MD5.md5s("KHy69gsk8%#@kl$kbb")
				+ "&act=kbb&fun=users&type=verifyPassword";
		PostRequest<String> postRequest = new PostRequest<String>(url,
				String.class, new OnVolleyResponseListener<String>() {

					private String phone_number;

					@Override
					public void onError(VolleyError volleyError) {
						UIUtil.DissDialog(context, waittingDialog);
						Toast.makeText(context, "提交失败！", Toast.LENGTH_SHORT)
								.show();

					}

					@Override
					public void onSuccess(String response) {
						UIUtil.DissDialog(context, waittingDialog);
						if (response != null) {
							JSONObject jsonObject;
							try {
								jsonObject = new JSONObject(response);
								String state = jsonObject.getString("state");
								if (state.equals("200")) {
									phone_number = getIntent().getStringExtra(
											"phone_number");

									Intent intent = new Intent(context,
											ChangePhoneNumberAt.class);
									intent.putExtra("phone_number",
											phone_number);
									startActivity(intent);
									finish();
								}
							} catch (JSONException e) {
								e.printStackTrace();
							}

						}
					}
				});

		HashMap<String, String> params = new HashMap<String, String>();
		params.put("phonenum", phonenum);// phonenum
		params.put("password", psw);// password
		postRequest.setIsParseJson(false);
		postRequest.setParams(params);
		executeRequest(postRequest);

	}

	@Override
	protected void initData() {
		// TODO Auto-generated method stub
		phonenum = getIntent().getStringExtra("phone_number");
		System.out.println("phonenum------" + phonenum);
	}

}
